<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->layout->setLayout('frontend/index');
    }

    public function index(){
        redirect(base_url().'frontend/home/listProduct/1');
    }

    public function listProduct($id){
        $page = $this->input->get('p');
        if (!$page) $page = 1;
        $list = $this->mproduct->get_product_by_category($id);
        $products = array();
        for($i=($page-1)*9;$i<count($list) && $i<($page)*9 ; ++$i)
        {
            array_push($products,$list[$i]);
        }
        $data['products'] = $products;
        $data['page'] = $page;

        $data['page_num'] = round(count($list)/9, 0, PHP_ROUND_HALF_DOWN) +1 ;
        $this->layout->view('frontend/home', $data);
    }

    public function search(){
        $query = $this->input->get('q');
        $data['query'] = $query;
        $page = $this->input->get('p');
        if (!$page) $page = 1;
        $list = $this->mproduct->search($query);
        $products = array();
        for($i=($page-1)*9;$i<count($list) && $i<($page)*9 ; ++$i)
        {
            array_push($products,$list[$i]);
        }
        $data['products'] = $products;
        $data['page'] = $page;

        $data['page_num'] = round(count($list)/9, 0, PHP_ROUND_HALF_DOWN) +1 ;
        $this->layout->view('frontend/home', $data);
    }

}